// pages/artificer/income_data/income_data.js
var api = require('../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: [{
        name: '今天',
        selected: true
      },
      {
        name: '昨天',
        selected: false
      },
      {
        name: '本月内',
        selected: false
      },
      {
        name: '总的',
        selected: false
      },
      {
        name: '自定义',
        selected: false
      },
    ],
    timeIndex: 0,
    star_time: '',
    end_time: '',
    timeBox: false,
    tab2: [{
        name: '收入',
        selected: true
      },
      // {
      //   name: '商城收入',
      //   selected: false
      // },
      {
        name: '支出',
        selected: false
      },
    ],
    tab2Index: 0,
    moneys: 0,
    orderCount: 0,
    page: 1,
    time: 0, //0=今天,1=昨天,2=本月内,3=总的, [Y-m-d - Y-m-d]=自定义  
    lists: [],
    page: 1,
    lastPage: 0,
    info: {},
    type: 0 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
    this.managerServices();
  },
  
  getAgent() {
    api.get('/manager/agent').then(res => {
      this.setData({
        info: res.detail
      })
    })
  },
  toShare(){
    wx.navigateTo({
      url: '/pages/invitation_poster/invitation_poster',
    })
  },
  bindDateChange(e) {
    let key = e.currentTarget.dataset.key;
    let val = e.detail.value;
    this.setData({
      [key]: val
    })
  },
  changeTopTab(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      tab2Index: index,
      lists: [],
      page: 1,
      lastPage: 0
    })
    if (index == 0) {
      this.managerServices();
    } else if (index == 1) {

    } else if (index == 2) {
      this.managerEexpends();
    }
  },
  commitTimeBox() {
    this.setData({
      time: this.data.star_time + " - " + this.data.end_time
    })
    this.managerServices()
    this.closeTimeBox();
  },
  closeTimeBox() {
    this.setData({
      timeBox: false
    })
  },

  //获取统计数据

  managerServices() {
    if (this.data.lastPage != 0 && this.data.page >= this.data.lastPage) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return false;
    }
    api.get('/manager/services', {
      type: this.data.type,
      time: this.data.time,
      page: this.data.page
    }).then(res => {
      this.setData({
        moneys: res.moneys,
        orderCount: res.order_count,
        lists: res.items,
        lastPage: res.lastPage,
        page: this.data.page + 1
      })
    })
  },
  //获取支出记录
  managerEexpends() {
    if (this.data.lastPage != 0 && this.data.page >= this.data.lastPage) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return false;
    }
    api.get('/manager/expends', {
      type: this.data.type,
      time: this.data.time,
      page: this.data.page
    }).then(res => {
      this.setData({
        moneys: res.moneys,
        orderCount: res.order_count,
        lists: res.items,
        lastPage: res.lastPage,
        page: this.data.page + 1
      })
    })
  },
  //商城收入
  managerGoods() {
    if (this.data.lastPage != 0 && this.data.page >= this.data.lastPage) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return false;
    }
    api.get('/manager/goods', {
      type: this.data.type,
      time: this.data.time,
      page: this.data.page
    }).then(res => {
      this.setData({
        lists: res.items,
        lastPage: res.lastPage,
        page: this.data.page + 1
      })
    })
  },
  selectTimeTab(e) {
    let index = e.currentTarget.dataset.index;
    if (index == 4) { //自定义
      this.setData({
        timeBox: true
      })
      return false;
    }
    this.setData({
      timeIndex: index,
      tab2Index: this.data.tab2Index,
      time: index,
      page: 1,
      lastPage: 0,
      lists: []
    })
    if (this.data.tab2Index == 0) {
      this.managerServices();
    } else if (this.data.tab2Index == 1) {
      this.managerGoods();
    } else if (this.data.tab2Index == 2) {
      this.managerEexpends();
    }
  },
  toSuser(){
    wx.navigateTo({
      url: '/pages/s_user/s_user',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获得dialog组件

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAgent();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})